function getJson_tablaInsertar(tBody,encabezados,mostrar){
    var json = '';
    $(tBody).children('.data').each( function(){ //Obtengo los TRs
        if(trHasDada($(this))){
            if(trAllRequiredFilled($(this))){
                if($('.has-error').length===0){
                    if(json.length===0){
                        json = parceTR($(this),encabezados);
                    } else {
                        json = json + ',' + parceTR($(this),encabezados);
                    }

                }else {
                    if(mostrar){
                        $('.alertText').html('<strong>Error!</strong> Some rows has wrong data. Please check.');
                        $('.alert').show();
                    }
                    json = '';
                    return false;
                }
            }else {
                if(mostrar){
                    $('.alertText').html('<strong>Error!</strong> Some rows has incomplete data.');
                    $('.alert').show();
                }
                json = '';
                return false;
            }
        }
    });
    return '{"data":['+json+']}';
}

function trHasDada(_tr){
    var hasData = false;
    _tr.children().each( function () {
        var element = $(this).children();
        if(element.is('input')){
            if($(element).val().length>0){
                hasData = true;
            }
        } else {
            if(!$(element).hasClass('noSave')){
                var input = $(element).children('input');
                if($(input).val().length>0){
                    hasData = true;
                }
            }
        }
    });
    return hasData;
}

function trAllRequiredFilled(_tr){
    var notFilled = false;
    _tr.children().each( function () {
        var element = $(this).children();
        if(element.is('input')){
            if($(element).hasClass('required') && $(element).val().length===0){
                notFilled = true;
            }
        } else {
            if(!$(element).hasClass('noSave')){
                var input = $(element).children('input');
                if($(input).hasClass('required') && $(input).val().length===0){
                    notFilled = true;
                }
            }
        }
    });
    return !notFilled;
}

function parceTR(_tr,encabezado) {
    var jsonTR = '';
    _tr.children().each( function (index, value) {
        var temp = '';
        var element = $(value).children();
        if(element.is('input')){
            temp = temp + '"'+encabezado[index]+'":"'+$(element).val()+'"';
        } else {
            var input = $(element).children('input');
            temp = temp + '"'+encabezado[index]+'":"'+$(input).val()+'"';
        }
        if(jsonTR.length===0){
            jsonTR = temp;
        } else {
            jsonTR = jsonTR + ',' + temp;
        }
    });
    return '{'+jsonTR+'}';
}

function ajaxPost(urlDest, dataDest, type,page){
    $('.cargando').show();
    $.ajax({
        method: "post",
        cache:false,
        timeout:8000,
        url: urlDest,
        data: dataDest,
        dataType: type,
        success:function(data){
            return manipularData(data,page);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(jqXHR);
            console.log(textStatus);
            console.log(errorThrown);
        },
        statusCode: {
            404: function() {
                $('.cargando').children('#mensaje').html("404: Resource not found" );
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            },
            403: function() {
                $('.cargando').children('#mensaje').html("403: Forbiden.");
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            },
            408: function(){
                $('.cargando').children('#mensaje').html("408: Request Timeout.");
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            },
            410: function() {
                $('.cargando').children('#mensaje').children('#mensaje').html("410: Not avaliable.");
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            },
            500: function () {
                $('.cargando').children('#mensaje').html("500: Server error.");
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            },
            501: function() {
                $('.cargando').children('#mensaje').html("501: Not implement.");
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            },
            503: function(){
                $('.cargando').children('#mensaje').html("503: Service is offline.");
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            }
        }
    });
}

function ajaxPost_Sinc(urlDest, dataDest, type,page){
    $('.cargando').show();
    $.ajax({
        method: "post",
        cache:false,
        timeout:8000,
        url: urlDest,
        data: dataDest,
        dataType: type,
        async: false,
        success:function(data){
            return manipularData(data,page);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(jqXHR);
            console.log(textStatus);
            console.log(errorThrown);
        },
        statusCode: {
            404: function() {
                $('.cargando').children('#mensaje').html("404: Resource not found" );
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            },
            403: function() {
                $('.cargando').children('#mensaje').html("403: Forbiden.");
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            },
            408: function(){
                $('.cargando').children('#mensaje').html("408: Request Timeout.");
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            },
            410: function() {
                $('.cargando').children('#mensaje').children('#mensaje').html("410: Not avaliable.");
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            },
            500: function () {
                $('.cargando').children('#mensaje').html("500: Server error.");
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            },
            501: function() {
                $('.cargando').children('#mensaje').html("501: Not implement.");
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            },
            503: function(){
                $('.cargando').children('#mensaje').html("503: Service is offline.");
                $('.cargando').dialog({buttons: { "Ok": {  text: 'Ok', class: 'btn btn-primary', click: function () {
                                $('.cargando').dialog( 'close' ); }}}});
            }
        }
    });
}